package cn.sample.moreitems.event;

import cn.sample.moreitems.MoreItems;
import cn.sample.moreitems.item.XuanyuanSwordItem;
import cn.sample.moreitems.util.Utils;
import com.google.common.collect.Sets;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.*;

@Mod.EventBusSubscriber(modid = MoreItems.MODID)
public class HandleTickEvent {

    public static Map<Vec3, List<ItemEntity>> itemEntityMap = new HashMap<>();
    public static Map<Vec3, List<LivingEntity>> livingEntityMap = new HashMap<>();
    private static Map<Vec3, Integer> countMap = new HashMap<>();


    @SubscribeEvent
    public static void onTickEvent(net.minecraftforge.event.TickEvent.LevelTickEvent event) {

        if (event.side.isClient()) {
            return;
        }

        if (event.phase == net.minecraftforge.event.TickEvent.Phase.END) {
            countMap.forEach((k, v) -> {
                if (v >= 10) {
                    itemEntityMap.remove(k);
                    livingEntityMap.remove(k);
                }
            });
            if (!itemEntityMap.isEmpty()) {
                countMap.entrySet().removeIf(v -> v.getValue() >= 10);
                itemEntityMap.forEach((k, items) -> {
                    countMap.compute(k, (kk, vv) -> {
                        if (vv == null) {
                            return 1;
                        } else {
                            return vv + 1;
                        }
                    });
                    items.forEach(v -> {
                        Utils.animateFlyTo(v, k);
                    });
                });
            }

            if (!livingEntityMap.isEmpty()) {
                countMap.entrySet().removeIf(v -> v.getValue() >= 10);
                livingEntityMap.forEach((k, items) -> {
                    countMap.compute(k, (kk, vv) -> {
                        if (vv == null) {
                            return 1;
                        } else {
                            return vv + 1;
                        }
                    });
                    items.forEach(v -> {
                        Utils.animateFlyTo(v, k);
                    });
                });
            }

        }
    }

    private static final Set<UUID> hasSword = Sets.newHashSet();

    @SubscribeEvent
    public static void onPlayerTick(net.minecraftforge.event.TickEvent.PlayerTickEvent e) {
        if (e.phase != TickEvent.Phase.END) return;
        Player player = e.player;
        if (player.level().isClientSide) return; // 只在服务端

        boolean nowHas = isHoldingSword(player);
        boolean had = hasSword.contains(player.getUUID());

        if (nowHas && !had) {
            XuanyuanSwordItem.applyArmor(player);
            hasSword.add(player.getUUID());
        } else if (!nowHas && had) {
            XuanyuanSwordItem.removeArmor(player);
            hasSword.remove(player.getUUID());
        }
    }

    private static boolean isHoldingSword(Player player) {
        return player.getMainHandItem().getItem() instanceof XuanyuanSwordItem ||
                player.getOffhandItem().getItem() instanceof XuanyuanSwordItem;
    }



}
